<template>
	<view class="content bg-white">
			<view class="head flex align-center padding-top" :style="'background-image: url(https://jianxia.mazhoudao.com/h5/static/me/bg@2x.png); background-position: top; background-repeat: no-repeat; background-size: contain; background-color: #6eb5f9'">
			<view class="left flex align-center margin-lr" @click="toLogin">
				<image class="logo" :src="head_url"></image>
			</view>
			<view class="text-white" @click="toLogin">
				{{userData.title1}}
			</view>
		</view>

		<!-- 微信授权登录 -->
		<!-- <button class="btn" open-type="getUserInfo" @getuserinfo="wxGetUserInfo">微信授权登录</button> -->

		<view class="list">
			<uni-list v-for="(item,index) in avatarList" :border="false" :key="index">
				<uni-list-item class="list-item" :title="item.text" :thumb="item.icon" link to="/pages/vue/index/index"
					:show-badge="true" :badge-text="item.num" badgeType="error" @click="click_goto_me(item,index)">
				</uni-list-item>
			</uni-list>
		</view>
	</view>
</template>

<script>
	import {
		getLogin as getLoginApi
	} from '@/api/login/loginTo.js'

	// import { loginMpWx as postLoginApi } from '@/api/login/loginTo.js'
	import storage from '@/utils/storage'
	// import {
	// 	N_Uid,
	// 	S_Headimgurl,
	// 	S_NickName
	// } from '@/store/mutation-types'
	// import {
	// 	checkLogin
	// } from '@/core/app'
	export default {
		data() {
			return {
				title: '',
				userData:{
					title1:''
				},
				SessionKey: '',
				OpenId: '',
				nickName: '',
				avatarUrl: '',
				isCanUse: uni.getStorageSync('isCanUse') || true,//默认为true
				head_url: "/static/me/head.png",
				avatarList: [{
						icon: 'https://su.bcebos.com/jianxia/icon1.png',
						text: "加点模拟",
						num: "",
						url: '/pages/me/jiadian/index',
					},
					{
						icon: 'https://su.bcebos.com/jianxia/icon1.png',
						text: "技能模拟",
						num: "",
						url: '/pages/me/jineng/index',
					},
					// {
					// 	icon: 'https://su.bcebos.com/jianxia/icon2.png',
					// 	text: "我的配装",
					// 	num: "",
					// 	url: '/pages/index/peizhuang/index?type=0',
					// },
					{
						icon: 'https://su.bcebos.com/jianxia/icon3.png',
						text: "我的收藏",
						num: "",
						url: '/pages/index/collect/index',
					},
					// {
					// 	icon: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png',
					// 	text: "消息",
					// 	num: "",
					// 	url: '/pages/index/peizhuang/index',
					// },
					// {
					// 	icon: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png',
					// 	text: "设置",
					// 	num: "",
					// 	url: '/pages/index/peizhuang/index',
					// },
					// {
					// 	icon: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png',
					// 	text: "我的配装",
					// 	num: "",
					// 	url: '/pages/me/feedback/index',
					// },
					// {
					// 	icon: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png',
					// 	text: "意见反馈",
					// 	num: "",
					// 	url: '/pages/me/feedback/index',
					// },
				]
			}
		},
		onLoad() {
			this.head_url = uni.getStorageSync('imgurl') ? uni.getStorageSync('imgurl') : "/static/me/head.png"
			this.userData.title1 = uni.getStorageSync('name') ? uni.getStorageSync('name') : "未登录"
			// storage.set(USER_ID, userId, expiryTime)
			// storage.set(ACCESS_TOKEN, token, expiryTime)
			// storage.set(SET_USER_NICKNAME, headurl, expiryTime)

			console.log('3333333')
		},
		methods: {
			headStyle(image) {
				console.log(image)
			},
			click_goto_me(item, index) {
				console.log('点击跳转',index)
				console.log('跳转',item,index)
				console.log('无路径跳转',this.avatarList[index].url)
				console.log('用户id',uni.getStorageSync('uid'))
				
				if(uni.getStorageSync('uid')=== ''){
					uni.showToast({
					    title: '请登录',
					    duration: 1000
					});
				}else{

					// console.log('11111111222',uni.getStorageSync('uid'))

					// console.log('路由',this.$router)

					uni.navigateTo({

						// let uid = uni.getStorageSync('uid'),
						// let page = 1,
						url: this.avatarList[index].url,
						// let params = {
						// 	cmd:'LunTan_PraiseInfo',
						// 	uid,
						// 	page
						// }

						// meCollectApi(params)


					})
				}



				// if (checkLogin()) {
				// 	uni.navigateTo({
				// 		url: this.avatarList[index].url
				// 	})
				// } else {
				// 	uni.showModal({
				// 		title: '友情提示',
				// 		content: '尚未登录请登录。',
				// 		success(res) {
				// 			if (res.confirm) {
				// 				uni.switchTab({
				// 					url: "/pages/me/index"
				// 				})
				// 			}
				// 		}
				// 	})
				// }
			},
			toLogin() {
				let _this = this;

				if (uni.getStorageSync('uid')) return
				
				let code = '';
				let params = '';
				let userProfile = {};
				
				// 第一步获取code
				uni.login({
					provider: 'weixin',
					success: (res) => {
						if (res.errMsg === 'login:ok') {
							code = res.code;
						}
					}
				})
				
				// 第二步与后端通信
				uni.getUserProfile({
					desc: '为了完善会员信息',
					success: (res) => {
						if (res.errMsg === 'getUserProfile:ok') {
							userProfile = JSON.parse(res.rawData);
							
							params = {
								cmd: 'login_code',
								sCode: code,
								avatar: userProfile.avatarUrl,
								username: userProfile.nickName
							}
							
							params = JSON.stringify(params);
							
							// 第一次请求
							getLoginApi(params).then(res => {
								// 不需要操作，具体逻辑写在下面的延迟请求里
							});
							
							// 延迟1.5秒后重复请求一次，具体原因不清楚
							setTimeout(() => {
								getLoginApi(params).then(res => {
									let data = res.data;

									if (data.length > 0) {
										// 业务逻辑
										uni.showToast({
										    title: '登录成功',
										    duration: 1500
										});
										this.$store.commit("SET_UID", data[0].nUid);
										this.$store.commit("SET_IMG", data[0].sHeadimgurl);
										this.$store.commit("SET_NAME", data[0].sNickName);
										this.$set(this.userData, 'title1', uni.getStorageSync('name'))
										this.head_url = uni.getStorageSync('imgurl')
									} else {
										uni.showToast({
											title: '登录失败',
											icon: 'error',
										})
									}
								});
							}, 1500);
						}
					}
				})
			}
		}
	}
</script>

<style scoped>
	.list {
		margin-top: 50rpx;
		width: 98%;
	}

	.content {
		flex-direction: column;
		align-items: center;
		justify-content: center;
		min-height: 100vh;
	}

	.bg-set {
		position: fixed;
		width: 100%;
		height: 368rpx;
		top: 0;
		left: 0;
		z-index: -1;
	}

	.head {
		width: 100%;
		height: 368rpx;
	}

	.head .left {
		height: 100%;
	}

	.head .left .logo {
		height: 114rpx;
		width: 114rpx;
		border-radius: 57rpx;
	}

	/* .btn {
		 float: left;
		display: flex;
		position: absolute;
		z-index: 1;
		top: 160rpx;
		left: 140rpx;
		background: none;
		border:none;
		color: white;

	} */

	/* .btn::after {
		border: none;
	} */
</style>
